<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 2016/11/9
 * Time: 上午10:08
 */
class ModTask
{
	
	public function getGradeData($params){
		$where = ' WHERE RecDate BETWEEN :t_s AND :t_e
                        AND Period=:period
                        AND GameSrc=:g';
		 
		$args = array(
				':t_s'    => $params['time_start'],
				':t_e'    => $params['time_end'],
				':g'      => empty($params['g']) ? 'ALL' : $params['g'],
				':period' => 'Daily'
		);
		 
		$user = AccAuth::me();
		if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
			$where .= " AND Platform=:p ";
			$args[':p'] = $params['os'];
		}
		 
		if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
			$modServer = new ModServer();
			$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
			$where .= " AND ( ";
			foreach ($list as $key=>$val){
				$where .= " SID = '{$val['develop_id']}'  or";
			}
			$where = substr($where, 0, -2);
			$where .= " ) ";
	
			$field = ' GameSrc,Level,SUM(PayUser) AS PayUser, SUM(PayCount) AS PayCount, SUM(PayMoney) AS PayMoney, SUM(PayGold) AS PayGold ';
			$group = ' GROUP BY GameSrc,Level ';
		}else{
			$where .= " AND SID=:ser ";
			$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
	
			$field = ' GameSrc,SID,Level,SUM(PayUser) AS PayUser, SUM(PayCount) AS PayCount, SUM(PayMoney) AS PayMoney, SUM(PayGold) AS PayGold ';
			$group = 'GROUP BY GameSrc,SID,Level';
		}
		$table = 'tblgamepaylevel';
	
		$sql = "SELECT {$field} FROM {$table} {$where} {$group}";
	
		$dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
		$res = $dbLink->query($sql, $args);
		 
		return $res;
	}
    public static function indexData($params){
        $table = 'tblGameTask';
        $where  = ' WHERE RecDate BETWEEN :begin AND :end 
        		       AND Period=:period
                       AND GameSrc=:game ';

        $args   = array(
            ':begin'    => $params['time_start'],
            ':end'      => $params['time_end'],
            ':period'   => 'Daily',
            ':game'     => $params['g'],
        );
        
        $user = AccAuth::me();
        if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
        	$where .= " AND Platform=:p ";
        	$args[':p'] = $params['os'];
        }

        $where .= ' AND ((TaskID="ALL") OR (TaskType=2)) ';
        
        if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
        	$modServer = new ModServer();
        	$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
        	$where .= " AND ( ";
        	foreach ($list as $key=>$val){
        		$where .= " SID = '{$val['develop_id']}'  or";
        	}
        	$where = substr($where, 0, -2);
        	$where .= " ) ";
        }else{
        	$where .= " AND SID=:ser ";
        	$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
        }

        $sql = "SELECT TaskType,TaskID,TaskName,
                  SUM(GetTaskNumber) AS GetTaskNumber, SUM(CompTaskNumber) AS CompTaskNumber,
                  SUM(GetTaskCnt) AS GetTaskCnt, SUM(CompTaskCnt) AS CompTaskCnt, SUM(LastGetTaskNumber) AS LastGetTaskNumber
                FROM {$table} {$where} 
                GROUP BY TaskType,TaskID,TaskName
                ORDER BY TaskType,CONVERT(TaskID,SIGNED)";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));

        return $dbLink->query($sql, $args);
    }
    
    
    public static function realIndexData($params){
    	$year_month = date('Ym');
    	$table = 'tblGameTask_'.$year_month;
    	$where  = ' WHERE RecDate=:recDate
        		       AND Period=:period
                       AND GameSrc=:game ';
    
    	$args   = array(
    			':recDate'    => $params['recDate'],
    			':period'   => '5mins',
    			':game'     => $params['g'],
    	);
    
    	$user = AccAuth::me();
    	if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
    		$where .= " AND Platform=:p ";
    		$args[':p'] = $params['os'];
    	}
    
    	$where .= ' AND ((TaskID="ALL") OR (TaskType=2)) ';
    
    	if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
    		$modServer = new ModServer();
    		$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
    		$where .= " AND ( ";
    		foreach ($list as $key=>$val){
    			$where .= " SID = '{$val['develop_id']}'  or";
    		}
    		$where = substr($where, 0, -2);
    		$where .= " ) ";
    	}else{
    		$where .= " AND SID=:ser ";
    		$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
    	}
    
    	$sql = "SELECT TaskType,TaskID,LastID,TaskName,
    	SUM(GetTaskNumber) AS GetTaskNumber, SUM(CompTaskNumber) AS CompTaskNumber,
    	SUM(GetTaskCnt) AS GetTaskCnt, SUM(CompTaskCnt) AS CompTaskCnt
    	FROM {$table} {$where}
    	GROUP BY TaskType,TaskID,LastID,TaskName
    	ORDER BY TaskType,CONVERT(TaskID,SIGNED)";
    
    	$dbName = ucfirst($params['g']).'RealTimeDB';
    	$dbLink = new PubDbLink('dbSelf', $dbName);
    	
//      	echo '<pre/>';
//      	var_dump($args);
//      	echo $sql;exit;
    
    	return $dbLink->query($sql, $args);
    }
}